package gemini.castor.ui.client.page.content.home.apply;

import gemini.basic.model.Distributor;
import gemini.castor.ui.client.mvp.container.CastorPlaceContainer;
import gemini.castor.ui.client.mvp.container.CastorPlaceContainer.PresenterGetter;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerDisplay;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerPresenter;
import gemini.castor.ui.client.mvp.gin.CastorGinjector;
import gemini.castor.ui.client.page.content.home.apply.distributorid.DistributorIDFormObject;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;
import ch.elca.gwt.mvp.client.place.PlaceRequest;

import com.google.inject.Inject;

public class ApplyPresenter extends
		CastorWidgetContainerPresenter<ApplyPresenter.Display> {

	public interface Display extends CastorWidgetContainerDisplay {
	}

	public static final Place PLACE = new CastorPlaceContainer<ApplyPresenter>(
			PlaceConstants.PLACE_APPLY, new PresenterGetter<ApplyPresenter>() {
				public ApplyPresenter getPresenter() {
					return CastorGinjector.INSTANCE.getApplyPresenter();
				}
			});

	@Inject
	public ApplyPresenter(Display display, EventBus eventBus) {
		super(display, eventBus);
		addPresenter(CastorGinjector.INSTANCE.getPersonalInfoFormPresenter());
		addPresenter(CastorGinjector.INSTANCE.getDistributorIDFormPresenter());
		addPresenter(CastorGinjector.INSTANCE.getContractAgreementFormPresenter());
		addPresenter(CastorGinjector.INSTANCE.getCheckOutFormPresenter());

	}

	/*
	 * Register listeners on Display actions
	 */
	@Override
	protected void onBind() {
		super.onBind();

		registerHandlerRegister();

		showDefaultPresenter();
	}

	private void registerHandlerRegister() {
		registerHandler(getLocalEventBus().addHandler(ApplyEvent.getType(),
				new ApplyHandler() {

					@Override
					public void onPersonalSubmit(ApplyEvent homeEvent) {
						DistributorIDFormObject dObject = (DistributorIDFormObject) getCurrentPresenter()
								.getOutputObject();

						showPresenter(CastorGinjector.INSTANCE
								.getDistributorIDFormPresenter(), dObject);
					}

					@Override
					public void onDistributorSubmit(ApplyEvent homeEvent) {
						Distributor distributor = (Distributor)getCurrentPresenter()
						.getOutputObject();
						showPresenter(CastorGinjector.INSTANCE
								.getContractAgreementFormPresenter(), distributor);

					}

					@Override
					public void onCheckOutSubmit(ApplyEvent applyEvent) {
						showDefaultPresenter();
					}

					@Override
					public void onAgreementSubmit(ApplyEvent applyEvent) {
						Object checkOutObject = getCurrentPresenter().getOutputObject();
						showPresenter(CastorGinjector.INSTANCE
								.getCheckOutFormPresenter(), checkOutObject);

					}

					@Override
					public void onCancel(ApplyEvent registerEvent) {
						showPresenter(CastorGinjector.INSTANCE.getPersonalInfoFormPresenter());
					}

					@Override
					public void onBack(ApplyEvent registerEvent) {
						showPresenter(CastorGinjector.INSTANCE
								.getDistributorIDFormPresenter());
					}

				}));
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	protected void onPlaceRequest(PlaceRequest request) {
		// TODO Auto-generated method stub
	}
}
